package com.iocup.keybastion.spring.starter.handler;


import com.iocup.keybastion.context.WebContextHolder;
import com.iocup.keybastion.spring.starter.config.JeeWebContextHolder;
import com.iocup.keybastion.utils.AuthUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class AuthorizeLogicCheckerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取处理method
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        Method method = ((HandlerMethod) handler).getMethod();
        WebContextHolder contextHolder = new JeeWebContextHolder(request,response);
        AuthUtils.getAuthorizer().check(contextHolder,method);
        return true;
    }
}
